package com.heima.test;

import com.heima.mapper.BrandMapper;
import com.heima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class selecrByConditions {
    /**
     * 3.条件查询;根据状态、公司名称、品牌名称来查询
     */
    @Test
    public void selectByCondition() throws Exception {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brands = mapper.selectByConditionSingle(brand);

        //方式三：使用map集合包装参数
        Map map=new HashMap();
//        map.put("status",status);
//        map.put("companyName",companyName);
//        map.put("brandName",brandName);
//        List<Brand> brands = mapper.selectByCondition(map);


        System.out.println(brands);
        sqlSession.close();
    }
}
